<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	    .errorText
	    {
	        color:red;
	    }
    </style>
</head>
<body>
	<form id="allocation_detail_add">
        <div style="padding-left:30px;padding-bottom:15px;padding-top: 15px;">
       	<input name="allocationId" class="mini-hidden" />
       	<input name="recallWhId" class="mini-hidden" />
            <table style="width:100%;" cellspacing="0" cellpadding="5">	
                <tr>
                    <td style="width:80px;">药品名称：</td>
                    <td style="width:150px;">
    					<input name="medicineId" errorMode="none" onvalidation="checkValidation"  requiredErrorText="请输入药品名称拼音首字母" onvaluechanged="checkMedicine();" class="mini-autocomplete" required="true" style="width:180px;" valueField="id" textField="name" url="allocation_detail_find_medicine.do" valueFromSelect="true" emptyText="请输入药品名称拼音首字母"/>
    				</td>
    				<td id="medicineId_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:80px;">数量：</td>
                    <td>
                    	<input name="quantity" maxValue="9999" minValue="1" changeOnMousewheel="false" class="mini-spinner" required="true" style="width:180px;" onvaluechanged="onValueChanged"/>
                    </td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding-top:50px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div> 
    </form>

<script type="text/javascript">
	mini.parse();
	
	var recallWhId = mini.getbyName("recallWhId");
	var medicine = mini.getbyName("medicineId");
	var allocationId = mini.getbyName("allocationId");
	
	//验证药品是否已经添加
	function checkMedicine(){
		var flag = true;
		$.ajax({
	        url: "allocation_detail_check_medicine.do",
			type: 'post',
			data: {medicineId: medicine.getValue(), allocationId: allocationId.getValue()},
			dataType: 'json',
	        cache: false,
	        async: false,
	        success: function (text) {
	            if(text.able){
	            	flag = true;
	            }else{
	            	mini.alert("所选药品已存在列表中！");
	            	flag = false;
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
		return flag;
	}
	
	var count = 0;
    function onValueChanged(e) {
        count++;
        document.title = count;
        var flag = true;
        if(medicine.getValue().length > 2){
	        $.ajax({
	        	url: "allocation_detail_check_inStock.do",
	        	type: 'post',
	        	data: {medicineId: medicine.getValue(), quantity: e.value, recallWhId: recallWhId.getValue()},
	        	dataType: 'json',
	        	cache: false,
	        	async: false,
	        	success: function (text) {
	        		if(text.able){
	        			flag = true;
		            }else{
		            	mini.alert("该药品库存量不足！");
		            	flag = false;
		            }
	        	},
	        	error: function (jqXHR, textStatus, errorThrown) {
		            alert(jqXHR.responseText);
		            window.CloseOwnerWindow();
		        }
	        });
        }
        return flag;
    }
	
	var form = new mini.Form("#allocation_detail_add");
	
	//保存数据
	function SaveData() {
	    var quantity = mini.getbyName("quantity");
	    var o = form.getData();
	    form.validate();
	    if (form.isValid() == false || checkMedicine() == false || onValueChanged(quantity) == false) return;
	    $.ajax({
	        url: "allocation_detail_save.do",
			type: 'post',
			dataType: 'json',
	        data: o,
	        cache: false,
	        success: function (text) {
	            if(text.success){
	            	window.CloseOwnerWindow("succ");
	            }else{
	            	mini.showTips({content:text.message,state:'danger',x:'center',y:'top'});
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
	}
	
	//标准方法接口定义
    function SetData(data) {
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            data = mini.clone(data);
            allocationId.setValue(data.id);
            recallWhId.setValue(data.recallWhId);
            form.setChanged(false);
        }
    }
	
    function GetData() {
        var o = form.getData();
        return o;
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }
	
    function updateError(e) {
        var id =e.sender.name + "_error";
        var el = document.getElementById(id);
        if (el) {
            el.innerHTML =e.errorText;
        }
    }
    
    function checkValidation(e) {                  
        updateError(e);
    }
</script>
</body>
</html>